package com.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.StudentMapper;
import com.pojo.Student;
import com.req.StudentPageReq;
import com.service.StudentService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    StudentMapper studentMapper;

    @Override
    public Student login(String account, String password) {
        Student student = studentMapper.login(account, password);
        return student;
    }

    @Override
    public boolean register(Student student) {
        // 校验用户名是否存在
        Student userByAccount = studentMapper.getUserByAccount(student.getAccount());
        if (userByAccount != null){
            return false;
        }
        int save = studentMapper.register(student);
        return save > 0;
    }

    @Override
    public Student getUserByAccount(String account) {
        Student student = studentMapper.getUserByAccount(account);
        return student;
    }

    @Override
    public boolean updatePassword(Student student) {
        int update = studentMapper.updatePassword(student);
        return update > 0;
    }

    @Override
    public Student getById(Integer id) {
        Student student = studentMapper.getById(id);
        return student;
    }

    @Override
    public boolean save(Student student) {
        // 校验用户名是否存在
        Student userByAccount = studentMapper.getUserByAccount(student.getAccount());
        if (userByAccount != null){
            return false;
        }
        student.setPassword("123456");
        studentMapper.save(student);
        return true;
    }

    @Override
    public void removeById(List<Integer> ids) {
        for (Integer id : ids){
            studentMapper.removeById(id);
        }
    }

    @Override
    public void updateById(Student student) {
        studentMapper.updateById(student);
    }

    @Override
    public PageInfo<Student> page(StudentPageReq studentPageReq) {
        // 设置分页参数
        PageHelper.startPage(studentPageReq.getPage(),studentPageReq.getPageSize());
        // 执行查询
        List<Student> students = studentMapper.selectStudentList(studentPageReq);
        // 获取分页信息
        return new PageInfo<Student>(students);
    }

    @Override
    public List<String> getMajorList() {
        return studentMapper.getMajorList();
    }

    @Override
    public List<String> getClassList() {
        return studentMapper.getClassList();
    }

    @Override
    public boolean changePassword(Student student) {
        // 更新密码
        student.setPassword(student.getNewPassword());
        Integer i = studentMapper.updateById(student);
        return i > 0;
    }

    @Override
    public boolean checkPassword(Student student) {
        // 检查密码是否正确
        Student student1 = studentMapper.checkPassword(student);
        if (student1 != null){
            return true;
        }
        return false;
    }
}
